package com.atguigu.tingshu.order.api;

import cn.hutool.db.sql.Order;
import com.atguigu.tingshu.common.login.GuiGuLogin;
import com.atguigu.tingshu.common.result.Result;
import com.atguigu.tingshu.common.util.AuthContextHolder;
import com.atguigu.tingshu.model.order.OrderInfo;
import com.atguigu.tingshu.order.service.OrderInfoService;
import com.atguigu.tingshu.vo.order.OrderInfoVo;
import com.atguigu.tingshu.vo.order.TradeVo;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@Tag(name = "订单管理")
@RestController
@RequestMapping("api/order")
@SuppressWarnings({"all"})
public class OrderInfoApiController {

	@Autowired
	private OrderInfoService orderInfoService;



	/**
	 * /api/order/orderInfo/findUserPage/1/10
	 * 我的订单列表
	 * @param page
	 * @param limit
	 * @return
	 */
	@GetMapping("/orderInfo/findUserPage/{page}/{limit}")
	@GuiGuLogin
	public Result<Page<OrderInfo>> findUserPage(@PathVariable Long page,
													  @PathVariable Long limit){

		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		//创建分页条件对象
		Page<OrderInfo> orderInfoPage=new Page<>(page,limit);
		//查询
		orderInfoPage=orderInfoService.findUserPage(orderInfoPage,userId);


		return Result.ok(orderInfoPage);
	}


	/**
	 *根据订单号获取订单信息
	 * /api/order/orderInfo/getOrderInfo/{orderNo}
	 * @param orderNo
	 * @return
	 */
	@GetMapping("/orderInfo/getOrderInfo/{orderNo}")
	@GuiGuLogin
	public Result<OrderInfo> getOrderInfo(@PathVariable String orderNo ){

		//获取用户id
		Long userId = AuthContextHolder.getUserId();
		//调用service查询
		OrderInfo orderInfo=orderInfoService.getOrderInfo(userId,orderNo);


		return Result.ok(orderInfo);

	}



	/**
	 * 提交订单
	 * api/order/orderInfo/submitOrder
	 * @param orderInfoVo
	 * @return
	 */
	@PostMapping("/orderInfo/submitOrder")
	@GuiGuLogin(required = true)
	public Result<Map<String,String>> submitOrder(@RequestBody OrderInfoVo orderInfoVo){
		//获取用户id
		Long userId = AuthContextHolder.getUserId();

		Map<String,String> orderMap=orderInfoService.submitOrder(orderInfoVo,userId);


		return Result.ok(orderMap);
	}

	/**
	 * 订单结算
	 * api/order/orderInfo/trade
	 * @param tradeVo
	 * @return
	 */
	@PostMapping("/orderInfo/trade")
	@GuiGuLogin(required = true)
	public Result<OrderInfoVo> trade(@RequestBody @Validated TradeVo tradeVo){
		//获取用户id
		Long userId = AuthContextHolder.getUserId();

		OrderInfoVo orderInfoVo=orderInfoService.trade(tradeVo,userId);

		return Result.ok(orderInfoVo);

	}


}

